﻿<html>

<head>
</head>

<body>
	<script type="text/javascript">
		function createThink(w, h) {
			var dir;
			var steps = new Array();
			var n = 0;
			/* dfs遍历数组 */
			var visited = new Array2(200);
			var first = 0;
			var dfsTrue = 0;

			// @brief：主函数
			// @retval: 蛇的方向
			return function (game) {
				var head = game.snake[0];
				var food = game.food[0];


				if (first === 0) {
					dfsTrue = 0;
					if (initArray() && getToFoodSteps(dir, head.x - 1, head.y, game, steps)) {
						dfsTrue = 1;
						steps.push('left');
					}
					else if (initArray() && getToFoodSteps(dir, head.x + 1, head.y, game, steps)) {
						dfsTrue = 1;
						steps.push('right');
					}
					else if (initArray() && getToFoodSteps(dir, head.x, head.y - 1, game, steps)) {
						dfsTrue = 1;
						steps.push('up');
					}
					else if (initArray() && getToFoodSteps(dir, head.x, head.y + 1, game, steps)) {
						dfsTrue = 1;
						steps.push('down');
					}
				}

				dir = steps[steps.length - first - 1];
				first += 1;
				if (steps.length === first) {
					//dfsTrue = 0;
					first = 0;
					initArray();
					steps = new Array();
					//debugger;
				}

				if (dfsTrue === 0) {
					//debugger;

					if (checkNext('up', game)) {
						dir = 'up';
					}
					if (checkNext('right', game)) {
						dir = 'right';
					}
					if (checkNext('down', game)) {
						dir = 'down';
					}
					if (checkNext('left', game)) {
						dir = 'left';
					}
				}
				return dir;
			}


			// @brief：创建长度为length正方形的二维数组
			// @retval: 数组引用
			function Array2(length) {
				var arr2 = new Array(length);

				for (var i = 0; i < arr2.length; i++) {
					arr2[i] = new Array(length);
				}
				return arr2;
			}

			// @brief：初始化二维数组
			// @retval: 数组引用
			function initArray() {
				for (var i = 0; i < visited.length; i++) {
					for (var j = 0; j < visited.length; j++) {
						visited[i][j] = 0;
					}
				}
				return true;
			}


			// @brief：从(x,y)点出发找到food的路径(使用dfs搜索当前点)
			// @args	
			//			dir:直接无视 
			//			steps:步法数组 
			// @retval: true 能, false 不能
			function getToFoodSteps(dir, x, y, game, steps) {
				/* ----------递归出口----------------------------- */
				/* (x,y)不安全 */
				if (check(({ x: x, y: y }), game) === false) {
					return false;
				}
				/* (x,y)已访问 */
				if (visited[x][y] === 1) {
					return false;
				}
				/* 找到食物 */
				if (game.food[0].x === x && game.food[0].y === y) {
					return true;
				}

				/* -----------递归体------------------------------- */
				visited[x][y] = 1;
				if (getToFoodSteps(dir, x - 1, y, game, steps))// 左
				{
					steps.push('left');
					return true;
				}
				if (getToFoodSteps(dir, x + 1, y, game, steps))// 右
				{
					steps.push('right');
					return true;
				}
				if (getToFoodSteps(dir, x, y - 1, game, steps))// 上
				{
					steps.push('up');
					return true;
				}
				if (getToFoodSteps(dir, x, y + 1, game, steps))// 下
				{
					steps.push('down');
					return true;
				}
				visited[x][y] = 0;

				return false;
			}

			function minDir(game) {
				var dir;
				var minValue = 999999;

				if (distanceAB('right', game) < minValue && checkNext('right', game)) {
					minValue = distanceAB('right', game);
					dir = 'right';
				}
				if (distanceAB('down', game) < minValue && checkNext('down', game)) {
					minValue = distanceAB('down', game);
					dir = 'down';
				}
				if (distanceAB('left', game) < minValue && checkNext('left', game)) {
					minValue = distanceAB('left', game);
					dir = 'left';
				}
				if (distanceAB('up', game) < minValue && checkNext('up', game)) {
					minValue = distanceAB('up', game);
					dir = 'up';
				}
				return dir;
			}

			function distanceAB(dir, game) {
				if (dir === 'right') {
					return Math.sqrt(
						Math.pow((game.snake[0].x + 1) - (game.food[0].x), 2) +
						Math.pow((game.snake[0].y) - (game.food[0].y), 2)
					);
				}

				if (dir === 'left') {
					return Math.sqrt(
						Math.pow((game.snake[0].x - 1) - (game.food[0].x), 2) +
						Math.pow((game.snake[0].y) - (game.food[0].y), 2)
					);
				}

				if (dir === 'up') {
					return Math.sqrt(
						Math.pow((game.snake[0].x) - (game.food[0].x), 2) +
						Math.pow((game.snake[0].y - 1) - (game.food[0].y), 2)
					);
				}

				if (dir === 'down') {
					return Math.sqrt(
						Math.pow((game.snake[0].x) - (game.food[0].x), 2) +
						Math.pow((game.snake[0].y + 1) - (game.food[0].y), 2)
					);
				}
			}

			function checkNext(dir, game) {
				if (dir === 'right') {
					//console.log(check(({ x: game.snake[0].x + 1, y: game.snake[0].y }), game));
					return check(({ x: game.snake[0].x + 1, y: game.snake[0].y }), game);
				}

				if (dir === 'left') {
					//console.log(check(({ x: game.snake[0].x - 1, y: game.snake[0].y }), game));
					return check(({ x: game.snake[0].x - 1, y: game.snake[0].y }), game);
				}

				if (dir === 'up') {
					//console.log(check(({ x: game.snake[0].x, y: game.snake[0].y - 1 }), game));
					return check(({ x: game.snake[0].x, y: game.snake[0].y - 1 }), game);
				}

				if (dir === 'down') {
					return check(({ x: game.snake[0].x, y: game.snake[0].y + 1 }), game);
				}
			}
		}

	</script>
</body>

</html>